import Layout from "@/layout/Layout"
import store from "@/store"

export const filterAsyncRouter = (asyncRouterMap)=> {
    return asyncRouterMap.filter(route => {
        //  delete route.name  name不唯一会导致router索引重复,从不菜单不可点击
        if (route.parentId === '' && route.sourceType === 1) { // 判断是否是一级菜单
            route.component = Layout;
            route.children = [{
                path: route.path + '/index',
                component: _import(route.route),
                meta: {
                    title: route.name,
                    hidden: route.hidden,
                    fullScreen: route.fullScreen
                }
            }];
            if (route.icon) {
                route.children[0].meta.icon = route.icon;
            }
            delete route.name;
        }

        if (route.sourceType === 2) { // 判断是否是目录
            if (route.parentId === '') {
                route.component = Layout;
            } else {
                route.component = _import(route.route);
            }

            route.meta = {
                title: route.name,
                hidden: route.hidden,
                fullScreen: route.fullScreen
            }
            if (route.icon) {
                route.meta.icon = route.icon;
            }
            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children)
            }
            delete route.name;
        }

        if (route.parentId !== '' && route.sourceType === 1) {  // 判断是否是子级菜单
            route.component = _import(route.route);
            route.meta = {
                title: route.name,
                hidden: route.hidden,
                fullScreen: route.fullScreen,
                buttons:[]
            }
            if (route.icon) {
                route.meta.icon = route.icon;
            }

            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children)
            }
            if(route.children && route.children.length > 0 && route.children[0].sourceType === 3){
                route.meta.buttons = JSON.parse(JSON.stringify(route.children));
                route.children = [];
            }

            delete route.name;
        }
        return true;
    });
}

/**
 * 导入组件
 * @param path
 * @return {function(*): any}
 * @private
 */
const _import = (path)=>{
    if(path!==''){
        return () => import('@/views' + path);
    }
    // return require('@/views/' + path).default
    // return (resolve) => require([`@/views${path}`], resolve);
}

/**
 * 扁平化树形数据
 * @param {Array} arrs 树形数据
 * @param {string} childs 树形数据子数据的属性名,常用'children'
 * @param {Array} attrArr 需要提取的公共属性数组(默认是除了childs的全部属性)
 * @returns
 */
export function extractTree(arrs,childs,attrArr){
    let attrList = [];
    if(!Array.isArray(arrs)&&!arrs.length)return [];
    if(typeof childs !== 'string')return [];
    if(!Array.isArray(attrArr)||Array.isArray(attrArr)&&!attrArr.length){
        attrList = Object.keys(arrs[0]);
        attrList.splice(attrList.indexOf(childs), 1);
    }else{
        attrList = attrArr;
    }
    let list = [];
    const getObj = (arr)=>{
        arr.forEach(function(row){
            let obj = {};
            attrList.forEach(item=>{
                obj[item] = row[item];
            });
            list.push(obj);
            if(row[childs]){
                getObj(row[childs]);
            }
        })
        return list;
    }
    return getObj(arrs);
}

/**
 * 获取根路由
 * @param data
 */
export const getRootRoute = (data) => {
  let arr =   extractTree(data,'children');
  for (let item of arr){
      if(item.sourceType === 1){
          return item.path;
      }
  }
}

/**
 * 获取根路由
 * @param data
 */
export const getRootRouteItem = (data) => {
    let arr =   extractTree(data,'children');
    for (let item of arr){
        if(item.sourceType === 1){
            return item;
        }
    }
}

/**
 * 清除缓存
 */
export const clearCache = (item) => {
    let funObj = {
        '创建新租表单':()=>{
            store.commit("SET_XIN_ZU_FORM", {});
            store.commit("SET_XIN_ZU_FORM_WUYE", {});
            store.commit("SET_XIN_ZU_FORM_YIZHAN", {});
            store.commit("SET_XIN_ZU_SUPPLEMENTARY_AGREEMENT_FORM", {});
        },
        '创建退租流程':()=>{
            store.commit("setTuiZuForm", {});
        },
        '物业验收':()=>{
            store.commit("setPropertyAcceptanceForm", {});
        },
        '资产信息录入':()=>{
            store.commit("setAssetInfoForm", {});
        },
        '房源信息录入':()=>{
            store.commit("setHousingResourcesForm", {});
        },
        '新增客户':()=>{
            store.commit("setCustomForm", {});
        },
        '新增物业':()=>{
            store.commit("setPropertyForm", {});
        }
    }
    if(item===undefined || item===''){
        for(let key in funObj){
            funObj[key]();
        }
        return;
    }
    if(funObj[item.title]!==undefined){
        funObj[item.title]();
    }
}
